#include "log.h"
#include "error_code.h"
#include <stdlib.h>
#include <stdio.h>

char g_log_prefix[3][8] = {"INFO","WARNING","ERROR"};
FILE *g_fp = NULL;
enum log_level g_log_level=0;//EMERGENCY

void set_log_level(enum log_level level)
{
	g_log_level=level;
}

int_32 write_log(enum log_type type,char* buff)
{
	FILE *fp=NULL;
	if(type<g_log_level)
	{
		return FDMS_ERR_LOG_LEVEL_NOT_ENOUGH;
	}
	fp=fopen(LOG_FILE,"a+");
	if(NULL == fp)
	{
		return FDMS_ERR_FILE_OPEN_FILED;
	}
	fprintf(fp,"[%s]:%s\n",g_log_prefix[type],buff);
	fclose(fp);
	return FDMS_SUCCESS;
}

int_32 init_log()
{
	g_fp=fopen(LOG_FILE,"a+");
	if(NULL == g_fp)
	{
		return FDMS_ERR_FILE_OPEN_FILED;
	}
	return FDMS_SUCCESS;
}

int_32 inner_write_log(enum log_type type,char* buff)
{
	if(type<g_log_level)
	{
		return FDMS_ERR_LOG_LEVEL_NOT_ENOUGH;
	}
	fprintf(g_fp,"[%s]:%s\n",g_log_prefix[type],buff);
	return FDMS_SUCCESS;
}

int_32 uninit_log()
{
	fclose(g_fp);
	return FDMS_SUCCESS;
}

